package com.chat.datea.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.chat.datea.R;
import com.chat.datea.adapter.Local_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.bean.MusicList;
import com.chat.datea.bean.local_music.LocalMusicEntity;
import com.chat.datea.ui.MainActivity;
import com.chat.datea.utils.mediautil.MediaUtil;

import java.util.ArrayList;

/**
 * Created by YanBinXie on 15/10/31.
 */
public class LocalSongFragment extends BaseFragment implements View.OnClickListener {

    private ListView listView;
    private ArrayList<LocalMusicEntity> musicEntities;
    private ImageView back;

    @Override
    protected void initData() {
        musicEntities = (ArrayList<LocalMusicEntity>) MediaUtil.getMp3Infos(getActivity());
        Local_Adapter local_adapter = new Local_Adapter(musicEntities, mActivity);
        listView.setAdapter(local_adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ArrayList<MusicList> lists = new ArrayList<>();
                for (LocalMusicEntity musicEntity : musicEntities) {
                    MusicList musicList = new MusicList();
                    musicList.setName(musicEntity.getTitle());
                    musicList.setSinger(musicEntity.getArtist());
                    musicList.setId(musicEntity.getId());
                    musicList.setAlbumId(musicEntity.getAlbum_id());
                    musicList.setUrl(musicEntity.getUrl());
                    musicList.setFrom("本地");
                    lists.add(musicList);
                }
                Toast.makeText(mActivity, "本地歌曲哦", Toast.LENGTH_SHORT).show();
                MainActivity.play(lists, position);
            }
        });

    }

    @Override
    protected void initView() {
        listView = (ListView) view.findViewById(R.id.local_list);
        back  = (ImageView) view.findViewById(R.id.back);
        back.setOnClickListener(this);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_local, null);
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
